package com.bpvse;
import org.bouncycastle.asn1.sec.SECNamedCurves;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.custom.sec.SecP256R1Curve;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECFieldElement;
import java.math.BigInteger;
import java.security.SecureRandom;
public class KeyGen {
        // 初始化曲线参数
      public static   ECDomainParameters params = new ECDomainParameters(SECNamedCurves.getByName("secp256r1"));
      public static ECCurve curve = params.getCurve();
      public  static ECPoint G = params.getG();

    public static BigInteger generatePrivateKey(){

        BigInteger n = params.getN();

        // 生成私钥
        SecureRandom random = new SecureRandom();
        BigInteger k = new BigInteger(n.bitLength(), random).mod(n);
        return k;
    }


    public static ECPoint generatePublicKey(BigInteger privateKey){
        // 计算公钥
        ECPoint kP = G.multiply(privateKey).normalize();
        return kP;
    }
//    public static void main(String[] args) {
//        // 初始化曲线参数
//        ECDomainParameters params = new ECDomainParameters(SECNamedCurves.getByName("secp256r1"));
//        ECCurve curve = params.getCurve();
//        ECPoint G = params.getG();
//        BigInteger n = params.getN();
//
//        // 生成私钥
//        SecureRandom random = new SecureRandom();
//        BigInteger k = new BigInteger(n.bitLength(), random).mod(n);
//
//        // 计算公钥
//        ECPoint kP = G.multiply(k).normalize();
//
//        System.out.println("Private Key (k): " + k.toString(16));
//        System.out.println("Public Key (kP): " + kP.getAffineXCoord().toBigInteger().toString(16) + ", " + kP.getAffineYCoord().toBigInteger().toString(16));
//    }
}
